﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace NewsSpider.App
{
    public partial class ViewArticleFrm : Form
    {
        public string strGetArticleID = "";
        NewsSpider.BLL.ArticlesSQLiteBLL asbll = new BLL.ArticlesSQLiteBLL();
        NewsSpider.DAL.ArticlesSQLiteDAL asdal = new DAL.ArticlesSQLiteDAL();

        public ViewArticleFrm(string strArticleID)
        {
            strGetArticleID = strArticleID;
            InitializeComponent();
        }

        private void ViewArticleFrm_Load(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = NewsSpider.Utility.CommonHelper.GetCurrentSoftSkin();
            this.Icon = new Icon(NewsSpider.Utility.ICONHelper.SetFormICON());

            try
            {
                SQLiteConnection cn = new SQLiteConnection(NewsSpider.DBSqlHelp.DBSQLiteHelper.ConnectionSQLite());
                cn.SetPassword(NewsSpider.Utility.CommonHelper.SQLITEPASSWORD);
                cn.Open();
                string strSql = "select * from Articles where article_id=" + strGetArticleID;
                SQLiteCommand cm = new SQLiteCommand(strSql, cn);
                SQLiteDataReader dr = cm.ExecuteReader();
                if (dr.Read())
                {
                    txtTitle.Text = dr["article_title"].ToString();
                    txtAuthor.Text = dr["article_author"].ToString();
                    txtSource.Text = dr["article_source"].ToString();
                    txtLink.Text = dr["article_link"].ToString();
                    txtContent.Text = dr["article_content"].ToString();
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                NewsSpider.Utility.Log.SaveErrorLog("读取Articles数据 -> " + ex.Message);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            NewsSpider.Model.Articles article = new Model.Articles();
            article.ArticleId = int.Parse(strGetArticleID);
            article.ArticleTitle = txtTitle.Text.Trim();
            article.ArticleAuthor = txtAuthor.Text.Trim();
            article.ArticleSource = txtSource.Text.Trim();
            article.ArticleLink = txtLink.Text.Trim();
            article.ArticleContent = txtContent.Text.Trim();
            asbll.UpdateArticlesBLL(article);
            MessageBox.Show("保存成功！");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            asbll.DeleteArticlesBLL("delete from Articles where article_id=" + strGetArticleID);
            this.Close();
        }
    }
}
